package br.ucb.mercadojogador.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import br.ucb.mercadojogador.beans.Time;
import br.ucb.mercadojogador.connection.FactoryConnection;

public class TimeDAOImpl implements TimeDAO{

	@Override
	public void insertTime(Time time) {
		
		Connection con = FactoryConnection.getConnection();
		PreparedStatement ps = null;
		String sql = "INSERT INTO TIME VALUES (?,?,?,?,?);";
		try {
			ps = con.prepareStatement(sql);
			ps.setInt(1,0);
			ps.setString(2,time.getNome());
			ps.setInt(3,time.getAnoFund());
			ps.setString(4,time.getEstado());
			ps.setDouble(5,time.getPatrimonio());
			ps.execute();		
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				if(ps!=null)
					ps.close();
				if(con!=null)
					con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}	
		}
	}

	@Override
	public void deleteTime(int idTime) {
		Connection con = FactoryConnection.getConnection();
		PreparedStatement ps = null;
		String sql = "DELETE FROM TIME WHERE idTime=?;";
		
		try {
			ps = con.prepareStatement(sql);
			ps.setInt(1,idTime);
			ps.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				if(ps!=null)
					ps.close();
				if(con!=null)
					con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}		
	}

	@Override
	public void updateTime(Time time) {
		Connection con = FactoryConnection.getConnection();
		PreparedStatement ps = null;
		String sql = "UPDATE TIME SET nome=?, anoFund=?, estado=?, patrimonio=? WHERE idTime=?;";
		try {
			ps = con.prepareStatement(sql);
			ps.setString(1,time.getNome());
			ps.setInt(2,time.getAnoFund());
			ps.setString(3,time.getEstado());
			ps.setDouble(4,time.getPatrimonio());
			ps.setInt(5,time.getIdTime());
			ps.execute();		
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				if(ps!=null)
					ps.close();
				if(con!=null)
					con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}	
		}
		
	}

	@Override
	public List<Time> findAllTimes() {
		Connection con = FactoryConnection.getConnection();
		PreparedStatement ps = null;
		String sql = "SELECT * FROM TIME;";
		ArrayList<Time> times = new ArrayList<Time>();
		Time time = null;
		
		try {
			ps = con.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			while(rs.next()){
				time = new Time();
				putTime(rs,time);
				times.add(time);				
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				if(ps!=null)
					ps.close();
				if(con!=null)
					con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}		
		return times;
	}
	
	private void putTime(ResultSet rs, Time time) throws SQLException{
		time.setIdTime(rs.getInt("IDTIME"));
		time.setNome(rs.getString("NOME"));
		time.setAnoFund(rs.getInt("ANOFUND"));
		time.setEstado(rs.getString("ESTADO"));
		time.setPatrimonio(rs.getDouble("PATRIMONIO"));
	}


	@Override
	public Time consultar(int idTime) {
		Connection con = FactoryConnection.getConnection();
		PreparedStatement ps = null;
		String sql = "SELECT * FROM TIME WHERE idTime=?;";
		Time time = null;
		
		try {
			ps = con.prepareStatement(sql);
			ps.setInt(1,idTime);
			ResultSet rs = ps.executeQuery();
			if(rs.next()){
				time = new Time();
				putTime(rs,time);				
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				if(ps!=null)
					ps.close();
				if(con!=null)
					con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}		
		return time;
	}


}
